const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({

  /**
   * 页面的初始数据
   */
  data: {},
  getUserProfile: function (e) {
    var _ = this;
    wx.getUserProfile({
      desc: '展示助力用户头像',
      success: res => {
        console.log(res.userInfo.avatarUrl)
        _.setData({
          avatarUrl: res.userInfo.avatarUrl
        })

        wx.showToast({
          title: '点击立即参与进入活动！',
          duration: 5000,
          icon: 'none'
        })
      }
    })
  },
  getPhoneNumber(e) {
    var _ = this;
    var area = _.data.area;
    var agent = _.data.agent;
    var flag = _.data.flag;
    if (flag) {
      wx.cloud.callFunction({
        name: 'getMobile',
        data: {
          weRunData: wx.cloud.CloudID(e.detail.cloudID),
        }
      }).then(res => {
        wx.setStorageSync('kyyqphone', res.result)
        _.setData({
          mobile: res.result,
        })
        wx.request({
          url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
          method: 'POST',
          data: {
            activeId: 25,
            mobile: res.result,
            areaCode: _.data.area,
            agent: _.data.agent,
            pageId: _.data.page
          },
          success: res => {

          }
        })
        wx.request({ //发送请求
          url: "https://zg99.offcn.com/index/kanjia/register/?actid=48617&callback=?",
          data: {
            phone: res.result,
            isagree: true,
            avatar: _.data.avatarUrl,
            area: area,
            agent: agent
          }, //接收参数
          success(res) { //返回
            var jsonStr = res.data;
            jsonStr = jsonStr.replace("(", "");
            jsonStr = jsonStr.replace(")", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
              var theJson = JSON.parse(jsonStr);
            };
            console.log(theJson)
            if (_.data.yqphone) {
              wx.request({ //发送请求
                url: "https://zg99.offcn.com/index/kanjia/writexz/?actid=48617&callback=?",
                data: {
                  xzphone: _.data.mobile,
                  phone: _.data.yqphone,
                  avatar: _.data.avatarUrl,
                  spid: 1
                }, //接收参数
                success(res) { //返回
                  var jsonStr = res.data;
                  jsonStr = jsonStr.replace("(", "");
                  jsonStr = jsonStr.replace(")", "");
                  if (typeof jsonStr != 'object') {
                    jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                    var theJson = JSON.parse(jsonStr);
                  };
                  console.log(theJson)
                  wx.navigateTo({
                    url: '../yq/yq?area=' + area + '&agent=' + agent + '&page=' + page,
                  })

                }
              })
            } else {
              wx.navigateTo({
                url: '../index/index',
              })
            }

          }
        })
      }).catch(err => {
        console.error(err);
      });
    } else {
      wx.showToast({
        title: '请先勾选隐私协议！',
        icon: 'none'
      })
    }

  },

  xz: function (e) {
    var _ = this;
    var flag = e.detail.value[0];
    if (flag == undefined) {
      flag = false
    } else {
      flag = true
    }
    _.setData({
      flag
    })
    console.log(_.data.flag)
  },
  xy: function (e) {
    var _ = this;
    wx.navigateTo({
      url: '../../agreement/agreement',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _ = this;
    console.log(options)
    wx.setStorageSync('area', options.area)
    wx.setStorageSync('agent', options.agent)
    wx.setStorageSync('page', options.page)
    _.setData({
      area: options.area,
      agent: options.agent,
    })
    if (options.page) {
      _.setData({
        page: options.page
      })
    }
    if (options.yqphone) {
      _.setData({
        yqphone: options.yqphone
      })
    }
    const kyyqphone = wx.getStorageSync('kyyqphone')
    if (kyyqphone) {
      wx.redirectTo({
        url: '../yq/yq?area=' + options.area + '&agent=' + options.agent + '&page=' + options.page,
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var _ = this;
    var area = wx.getStorageSync('area');
    var agent = wx.getStorageSync('agent');
    var page = wx.getStorageSync('page');
    return {
      title: '考研路上，与子偕行！',
      path: 'pages/kyyq/index/index?area=' + area + '&agent=' + agent + '&page=' + page,
      success: function (res) {
        console.log(res)
      }
    }
  }
})